package com.woniu.cinema.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.woniu.common.annotation.Log;
import com.woniu.common.core.controller.BaseController;
import com.woniu.common.core.domain.AjaxResult;
import com.woniu.common.enums.BusinessType;
import com.woniu.cinema.domain.Comment;
import com.woniu.cinema.service.CommentService;
import com.woniu.common.utils.poi.ExcelUtil;
import com.woniu.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 影片评论Controller
 *
 * @author yy
 * @date 2023-09-27
 */
@RestController
@RequestMapping("/cinema/comment")
public class CommentController extends BaseController
{
    @Autowired
    private CommentService commentService;

    /**
     * 查询影片评论列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Comment comment)
    {
        startPage();
        List<Comment> list = commentService.selectCommentList(comment);
        return getDataTable(list);
    }
    @GetMapping("web/list")
    public TableDataInfo weblist(Comment comment)
    {
        startPage();
        List<Comment> list = commentService.selectCommentList(comment);
        return getDataTable(list);
    }
    /**
     * 导出影片评论列表
     */
    @Log(title = "影片评论", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Comment comment)
    {
        List<Comment> list = commentService.selectCommentList(comment);
        ExcelUtil<Comment> util = new ExcelUtil<Comment>(Comment.class);
        return util.exportExcel(list, "comment");
    }

    /**
     * 获取影片评论详细信息
     */
    @GetMapping(value = "/{commentId}")
    public AjaxResult getInfo(@PathVariable("commentId") Integer commentId)
    {
        return AjaxResult.success(commentService.selectCommentById(commentId));
    }

    @GetMapping(value = "web/queryCommentByUserIdAndMovieId")
    public AjaxResult queryCommentByUserIdAndMovieId(Long movieId, HttpServletRequest request)
    {
        String userIdStr = request.getHeader("userId");
        Long userId = null;
        if(userIdStr != null)
        {
            userId = Long.parseLong(userIdStr);

        }
        return AjaxResult.success(commentService.queryCommentByUserIdAndMovieId(userId, movieId));
    }

    /**
     * 新增影片评论
     */
    @Log(title = "影片评论", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Comment comment)
    {
        return toAjax(commentService.insertComment(comment));
    }



    @PostMapping("web/add")
    public AjaxResult webadd(@RequestBody Comment comment,HttpServletRequest request)
    {
        String userIdStr = request.getHeader("userId");
        Long userId = null;
        if(userIdStr != null)
        {
            userId = Long.parseLong(userIdStr);
            comment.setUserId(userId);
            comment.setUserAccount(request.getHeader("userAccount"));
        }
        return toAjax(commentService.insertComment(comment));
    }

    /**
     * 修改影片评论
     */
    @Log(title = "影片评论", businessType = BusinessType.UPDATE)
    @PostMapping("web/edit")
    public AjaxResult webedit(@RequestBody Comment comment)
    {

        return toAjax(commentService.updateComment(comment));
    }

    @PutMapping
    public AjaxResult edit(@RequestBody Comment comment)
    {
        return toAjax(commentService.updateComment(comment));
    }

    /**
     * 删除影片评论
     */
    @Log(title = "影片评论", businessType = BusinessType.DELETE)
	@DeleteMapping("/{commentIds}")
    public AjaxResult remove(@PathVariable Integer[] commentIds)
    {
        return toAjax(commentService.deleteCommentByIds(commentIds));
    }
}
